require 'javaobs'
require 'base64'
class BinaryBufferedIO
  def buffer
    @buffer
  end
  def initialize(buf)
    @buffer = buf
    @pos = 0
  end
  
  def reset
    @pos = 0
  end
  def eof
    @pos==@buffer.size
  end
  
  def putc(b)
    @buffer=@buffer+[b].pack('c')
  end
  def write(op)
    @buffer=@buffer+op
  end
  def read(size=1, buffer = nil)
    raise "end of file" if(eof)
    if (@buffer.size - @pos) < size then
      size=@buffer.size - @pos
    end
    result = @buffer[@pos,size]
    @pos += result.size
    buffer.replace(result) if buffer
    result
  end
  
  def pos
    @pos
  end
  
  def pos=(newpos)
    seek(newpos) 
  end
  
  def seek(amount, whence=IO::SEEK_SET)
    case whence
    when IO::SEEK_CUR
      curr=@pos+amount
      
    when IO::SEEK_END
      curr=@buffer.size+amount
      
    when IO::SEEK_SET
      curr = amount
    end
    raise "rewind before buffer start" if (curr < 0)
    raise "beyond buffer end" if (curr >= @buffer.size)
    @pos=curr
    curr
  end
end
class Util
  @@m_socketAddress=nil
  @@m_socketAddress2=nil
  
  def Util.getBytes(obj)
    data=''
    f=BinaryBufferedIO.new(data)
    os = Java::ObjectOutputStream.new(f)
    os.writeObjects(obj)
    f.buffer
  end
  def Util.bin2Object(bin)
    f=BinaryBufferedIO.new(bin)
    os = Java::ObjectInputStream.new(f)
    os.readObjects
  end
  def Util.twoSocketAddress(addr1=nil,addr2=nil)
    if(@@m_socketAddress2==nil) then
      b64 = <<BIN
rO0ABXVyABlbTGphdmEubmV0LlNvY2tldEFkZHJlc3M75KnoJbQoHyECAAB4
cAAAAAJzcgAaamF2YS5uZXQuSW5ldFNvY2tldEFkZHJlc3NGcZRhb/mqRQIA
A0kABHBvcnRMAARhZGRydAAWTGphdmEvbmV0L0luZXRBZGRyZXNzO0wACGhv
c3RuYW1ldAASTGphdmEvbGFuZy9TdHJpbmc7eHIAFmphdmEubmV0LlNvY2tl
dEFkZHJlc3NIYfZi9JedagIAAHhwAAAUHnNyABRqYXZhLm5ldC5JbmV0QWRk
cmVzcy2bV6+f4+vbAgADSQAHYWRkcmVzc0kABmZhbWlseUwACGhvc3ROYW1l
cQB+AAR4cH8AAAEAAAACcHBzcQB+AAIAABQfc3EAfgAHyiZfxwAAAAJwcA==
BIN
      data=Base64.decode64(b64)
      @@m_socketAddress2 = bin2Object(data)
    end
    objs=[@@m_socketAddress2[0].clone]
    if( objs!=nil)
      if(addr1!=nil)
        objs[0][0].port=addr1[1]
        ipl=0
        addr1[3].split('.').each do |w|
          ipl=ipl*256+w.to_i
        end
        objs[0][0].addr.address=ipl
      end
      if(addr2!=nil)
        objs[0][1].port=addr2[1]
        ipl=0
        addr2[3].split('.').each do |w|
          ipl=ipl*256+w.to_i
        end
        objs[0][1].addr.address=ipl
      end
    end
    #p objs
    objs
  end
  @@m_siglmessage=nil
  def Util.getReponse(sType=nil)
    if(@@m_siglmessage==nil) then
    b64 = <<BIN
rO0ABXNyACFuZXQubnV0c3Muc2lnbmFsaW5nLkpTbGluZ01lc3NhZ2UAAAAA
AAAAAAIABkkAAmlkTAALZGVzdGluYXRpb250AA5MamF2YS9uZXQvVVJJO0wA
B3BheWxvYWR0ABJMamF2YS9sYW5nL09iamVjdDtMAAhyZXNwb25zZXQALExu
ZXQvbnV0c3Mvc2lnbmFsaW5nL0pTbGluZ01lc3NhZ2UkUmVzcG9uc2U7TAAG
c291cmNlcQB+AAFMAAR0eXBldAAoTG5ldC9udXRzcy9zaWduYWxpbmcvSlNs
aW5nTWVzc2FnZSRUeXBlO3hwLp5r+3NyAAxqYXZhLm5ldC5VUkmsAXguQ55J
qwMAAUwABnN0cmluZ3QAEkxqYXZhL2xhbmcvU3RyaW5nO3hwdAANZWNob0Bl
Y2hvLm5ldHh1cgAZW0xqYXZhLm5ldC5Tb2NrZXRBZGRyZXNzO+Sp6CW0KB8h
AgAAeHAAAAACc3IAGmphdmEubmV0LkluZXRTb2NrZXRBZGRyZXNzRnGUYW/5
qkUCAANJAARwb3J0TAAEYWRkcnQAFkxqYXZhL25ldC9JbmV0QWRkcmVzcztM
AAhob3N0bmFtZXEAfgAHeHIAFmphdmEubmV0LlNvY2tldEFkZHJlc3NIYfZi
9JedagIAAHhwAAAUHnNyABRqYXZhLm5ldC5JbmV0QWRkcmVzcy2bV6+f4+vb
AgADSQAHYWRkcmVzc0kABmZhbWlseUwACGhvc3ROYW1lcQB+AAd4cH8AAAEA
AAACcHBzcQB+AAwAABQfc3EAfgAQyiZfxwAAAAJwcH5yACpuZXQubnV0c3Mu
c2lnbmFsaW5nLkpTbGluZ01lc3NhZ2UkUmVzcG9uc2UAAAAAAAAAABIAAHhy
AA5qYXZhLmxhbmcuRW51bQAAAAAAAAAAEgAAeHB0AA5KU0xJTkdfU1VDQ0VT
U3NxAH4ABnQADm1lQDIwMi4zOC42NC4xeH5yACZuZXQubnV0c3Muc2lnbmFs
aW5nLkpTbGluZ01lc3NhZ2UkVHlwZQAAAAAAAAAAEgAAeHEAfgAVdAAMSlNM
SU5HX1JPVVRF
BIN
      data=Base64.decode64(b64)
      @@m_siglmessage = bin2Object(data)[0]
    end
    #p objs
    objs=@@m_siglmessage.response.clone
    objs.cspcspcsp=sType if(sType!=nil)
    objs
  end  
  def Util.socketAddress(addr=nil)
    if(@@m_socketAddress==nil) then
      b64 = <<BIN
rO0ABXNyABpqYXZhLm5ldC5JbmV0U29ja2V0QWRkcmVzc0ZxlGFv+apFAgAD
SQAEcG9ydEwABGFkZHJ0ABZMamF2YS9uZXQvSW5ldEFkZHJlc3M7TAAIaG9z
dG5hbWV0ABJMamF2YS9sYW5nL1N0cmluZzt4cgAWamF2YS5uZXQuU29ja2V0
QWRkcmVzc0hh9mL0l51qAgAAeHAAABErc3IAFGphdmEubmV0LkluZXRBZGRy
ZXNzLZtXr5/j69sCAANJAAdhZGRyZXNzSQAGZmFtaWx5TAAIaG9zdE5hbWVx
AH4AAnhwfwAAAQAAAAJwcA==
BIN
      data=Base64.decode64(b64)
      @@m_socketAddress = bin2Object(data)
    end
    objs=[@@m_socketAddress[0].clone]
    #p objs
    if(addr!=nil && objs!=nil) then
      objs[0].port=addr[1]
      ipl=0
      addr[3].split('.').each do |w|
        ipl=ipl*256+w.to_i
      end
      objs[0].addr=objs[0].addr.clone
      objs[0].addr.address=ipl>=0x80000000?ipl-0x100000000:ipl
    end
    #p objs
    objs
  end
end
if $0==__FILE__ then
  #  Test.socketAddress
  addr=["AF_INET", 5150, "localhost", "127.0.0.1"]
  obj=    Util.socketAddress( addr)
  p obj
  p Util.getBytes(obj)
  addr2=["AF_INET", 5151, "localhost", "202.38.95.199"]
  obj=    Util.twoSocketAddress( addr,addr2)
  p obj
#  p Base64.encode64(Util.getBytes(obj))
  p Util.twoSocketAddress
end